package com.songsong.myblog2.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.songsong.myblog2.domain.Categories;
import com.songsong.myblog2.domain.User;
import com.songsong.myblog2.mapper.CategoriesMapper;
import com.songsong.myblog2.service.CategoriesService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service(value = "categoriesService")
public class CategoriesServiceImpl implements CategoriesService {

    //注入mapper
    @Resource(name = "categoriesMapper")
    private CategoriesMapper categoriesMapper;

    @Override
    public void addCategories(Categories categories) {
        //调用mapper
        categoriesMapper.addCategories(categories);
    }

    @Override
    public Categories selectOne(String categoriesName) {

        return categoriesMapper.selectOne(categoriesName);
    }

    @Override
    //获取分类管理
    public JSONObject getCategoriesManagement(int rows, int pageNum) {

        PageHelper.startPage(pageNum, rows);
        List<Categories> categoriesList = categoriesMapper.getCategoriesManagement();
        PageInfo<Categories> pageInfo = new PageInfo<>(categoriesList);
        JSONArray returnJsonArray = new JSONArray();
        JSONObject returnJson = new JSONObject();
        JSONObject userJson;
        for (Categories categories : categoriesList) {
            userJson = new JSONObject();
            userJson.put("categoriesName", categories.getCategoriesName());
            userJson.put("categoriesNum", categories.getCategoriesNum());
            userJson.put("categoriesData", categories.getCategoriesData());
            userJson.put("categoriesStatus", categories.getCategoriesStatus());
            returnJsonArray.add(userJson);
        }
        returnJson.put("status", 200);
        returnJson.put("result", returnJsonArray);
        JSONObject pageJson = new JSONObject();
        pageJson.put("pageNum", pageInfo.getPageNum());
        pageJson.put("pageSize", pageInfo.getPageSize());
        pageJson.put("total", pageInfo.getTotal());
        pageJson.put("pages", pageInfo.getPages());
        pageJson.put("isFirstPage", pageInfo.isIsFirstPage());
        pageJson.put("isLastPage", pageInfo.isIsLastPage());
        returnJson.put("pageInfo", pageJson);
        return returnJson;

    }

    @Override
    public void categoriesToStop(String categoriesName) {
        categoriesMapper.categoriesToStop(categoriesName);
    }

    @Override
    public void categoriesToBegin(String categoriesName) {
        categoriesMapper.categoriesToBegin(categoriesName);
    }

    @Override
    public JSONObject findCategoriesNameAndArticleNum() {

        List<Categories> categoriesList = categoriesMapper.getCategories();
        JSONObject categoryJson;
        JSONArray categoryJsonArray = new JSONArray();
        JSONObject returnJson = new JSONObject();
        for(Categories categories : categoriesList){
            categoryJson = new JSONObject();
            categoryJson.put("categoryName",categories.getCategoriesName());
            categoryJson.put("categoryArticleNum",categories.getCategoriesNum());
            categoryJsonArray.add(categoryJson);
        }
        returnJson.put("status",200);
        returnJson.put("result",categoryJsonArray);
        return returnJson;
    }

    @Override
    public void updateCategoriesNum(String categoriesName) {
        categoriesMapper.updateCategoriesNum(categoriesName);
    }

    @Override
    public List<Categories> getCategories() {

        return categoriesMapper.getCategories();

    }

    @Override
    public int getCategoriesNum() {
        return categoriesMapper.getCategoriesNum();
    }
}
